//
//  MCFreightViewController.swift
//  StoreManage
//
//  Created by MC on 2018/9/20.
//  Copyright © 2018年 MC. All rights reserved.
//

import UIKit

protocol MCFreightViewControllerDelegate : NSObjectProtocol {
    func MCFreightViewControllerSelectedFreightTemplate(model:MCFreightModel)
}

class MCFreightViewController: MCViewController {
    
    var currentFreightTemplateId : Int = 0   // 当前运费模板Id
    
    weak var delegate : MCFreightViewControllerDelegate?
    

    
    
    private var pageNum = 1
    
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        pageNum = 1
        sendNetworking()
    }
    
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        baseSetting()
        
        initUI()        
    }
    
    // MARK: - Setter & Getter
    lazy var tableView: MCTableView = {
        let tableView = MCTableView.init(frame: CGRect.zero, style: UITableViewStyle.plain)
        tableView.delegate = self
        tableView.dataSource = self
        return tableView
    }()

    lazy var dataArrayM: NSMutableArray = {
        let arrayM = NSMutableArray()
        return arrayM
    }()
}

//MARK: 通知回调，闭包回调，点击事件
extension MCFreightViewController {
    @objc func rightItemClicked() {
        let vc = MCAddFreightViewController()
        MCPushToViewController(current: self, target: vc, isHiddenBottomBarWhenBack: true)
    }
}


//MARK: 网络请求
extension MCFreightViewController {
    
    func sendNetworking() {
        
        let para = [
            "pageNum" : pageNum
            ] as [String : Any]

        

        MCNetwork.POST(JAVA_findFreightTemplateInfoPage, para, vc: self, success: { (data)
            in
            
            if self.pageNum == 1 {
                self.dataArrayM.removeAllObjects()
                self.tableView.mj_footer.resetNoMoreData()
            }
            
            self.tableView.mj_header.endRefreshing()
            self.tableView.mj_footer.endRefreshing()
            
            let array = data.arrayObject ?? []
            
            if array.count == 0 {
                self.tableView.mj_footer.endRefreshingWithNoMoreData()
            }
            
            if array.count + self.dataArrayM.count == 0 {
                self.tableView.showBgImage()
            } else {
                self.tableView.hideBgImage()
                for dict in array {
                    let model = MCFreightModel()
                    model.setValuesForKeys(dict as? [String:Any] ?? [:])
                    
                    if model.freightTemplateId.intValue == self.currentFreightTemplateId {
                        model.b = true
                    }
                    
                    self.dataArrayM.add(model)
                }
            }

            self.tableView.reloadData()
        }, failure: nil)
    }
}

//MARK: UI的处理,通知的接收
extension MCFreightViewController {
    
    func baseSetting() {
        self.title = "运费模板"
        navigationItem.rightBarButtonItem = MCSetBarButtonItem_text("新增", textColor:UIColor.MCMain, target: self, selector: #selector(rightItemClicked))
        
        tableView.mj_header = MCRefreshHeader.init(refreshingBlock: {
            self.pageNum = 1
            self.sendNetworking()
        })
        
        tableView.mj_footer = MCRefreshFooter.init(refreshingBlock: {
            self.pageNum += 1
            self.sendNetworking()
        })
    }
    
    
    func initUI() {
        view.addSubview(tableView)
        tableView.snp.remakeConstraints { (make) ->Void in
            make.edges.equalTo(view).inset(UIEdgeInsetsMake(0, 0, MCSafeAreaBottomHeight, 0))
        }

    }
}

extension MCFreightViewController: UITableViewDelegate,UITableViewDataSource {
    
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return dataArrayM.count
    }
    
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        let model = dataArrayM[indexPath.row] as! MCFreightModel
        
        
        if model.isShow {
            return model.cellHeight
        } else {
            return 97
        }
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        
        let cell = MCFreightCell.customCell(tableView: tableView)
        cell.model = dataArrayM[indexPath.row] as! MCFreightModel
        
        cell.closure = { () -> Void in
            self.tableView.reloadData()
        }
        
        cell.editClosure = { model in
            let vc = MCAddFreightViewController()
            vc.model = model
            MCPushToViewController(current: self, target: vc)
        }
        
        return cell
    }
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        let model = self.dataArrayM[indexPath.row] as? MCFreightModel
        delegate?.MCFreightViewControllerSelectedFreightTemplate(model: model!)
        
        self.navigationController?.popViewController(animated: true)
    }
}

